//控制层
app.controller('typeTemplateController', function ($scope, $controller, typeTemplateService, brandService, specificationService) {

    $controller('baseController', {$scope: $scope});//继承


    //扩展属性删除行
    $scope.deleTableRow = function (index) {
        $scope.entity.customAttributeItems.splice(index, 1);//删除
    }

    //扩展属性新增行

    $scope.addTableRow = function () {
        $scope.entity.customAttributeItems.push({});
    }

    //select2标签需要的语法是id和text，但是我们的品牌表里面是id和name，需要把name转成text，命别名就OK啦
    //需要的是{data:[集合]}
    //$scope.brandList={data:[{id:1,text:'联想'},{id:2,text:'华为'},{id:3,text:'小米'}]};

    $scope.brandList = {data: []};
    //因为我们查询的是品牌，所以需要去品牌的service里面去，也就是说，需要在模板的controller里面出入品牌的service
    $scope.specList = {data: []};


    $scope.findspecList = function () {
        specificationService.getSpecList().success(function (response) {
            $scope.specList = {data: response}
        });
    }

    $scope.findBrandList = function () {
        brandService.getBrandList().success(function (response) {
            $scope.brandList = {data: response}
        });
    }

    //读取列表数据绑定到表单中  
    $scope.findAll = function () {
        typeTemplateService.findAll().success(
            function (response) {
                $scope.list = response;
            }
        );
    }

    //分页
    $scope.findPage = function (page, rows) {
        typeTemplateService.findPage(page, rows).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }

    //查询实体
    $scope.findOne = function (id) {
        typeTemplateService.findOne(id).success(
            function (response) {
                $scope.entity = response;
                $scope.entity.brandIds = JSON.parse($scope.entity.brandIds);
                $scope.entity.specIds = JSON.parse($scope.entity.specIds);
                $scope.entity.customAttributeItems = JSON.parse($scope.entity.customAttributeItems);
            }
        );
    }

    //保存,这个是在前台判断，后台把新增和修改分开的
    $scope.save = function () {
        var serviceObject;//服务层对象
        if ($scope.entity.id != null) {//如果有ID
            serviceObject = typeTemplateService.update($scope.entity); //修改
        } else {
            serviceObject = typeTemplateService.add($scope.entity);//增加
        }
        serviceObject.success(
            function (response) {
                if (response.success) {
                    //重新查询
                    $scope.reloadList();//重新加载
                } else {
                    alert(response.message);
                }
            }
        );
    }


    //批量删除
    $scope.dele = function () {
        //获取选中的复选框
        typeTemplateService.dele($scope.selectIds).success(
            function (response) {
                if (response.success) {
                    $scope.reloadList();//刷新列表
                }
            }
        );
    }

    $scope.searchEntity = {};//定义搜索对象

    //搜索
    $scope.search = function (page, rows) {
        typeTemplateService.search(page, rows, $scope.searchEntity).success(
            function (response) {
                var TemList = response.rows;
                $scope.list = TemList;
                for (var i = 0; i < TemList.length; i++) {
                    var brandIds = $scope.JSONtoParse(TemList[i].brandIds, "text");
                    TemList[i].brandIds = brandIds;
                    var specIds = $scope.JSONtoParse(TemList[i].specIds, "text");
                    TemList[i].specIds = specIds;
                    var customAttributeItems = $scope.JSONtoParse(TemList[i].customAttributeItems, "text");
                    TemList[i].customAttributeItems = customAttributeItems;
                }

                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }

});	
